﻿using System;
using System.Linq;
using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class UplineInstructionProcessorFacade
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(UplineInstructionProcessorFacade));
        public void Process(string cn)
        {
            try
            {
                if (string.IsNullOrEmpty(cn)) return;
                using (var gCtx = new GatewayMonitorContext())
                using (var pCtx = new PlatformContext())
                {
                    //DownlineFlowTriggerMonitor.Trigger(pCtx,gCtx);
                    var flag = ProcessFlag.NotProcessed.ToString();
                    var items =
                        gCtx.received_msg.Where(t => t.is_length_right)
                            .Where(t => t.processed_flag.Equals(flag))
                            .Where(t => t.cn.Equals(cn))
                            .OrderBy(t => t.datatime)
                            .Take(100)
                            .ToList();
                    foreach (var item in items)
                    {
                        UplineInstructionProcess(cn, item, pCtx, gCtx);
                        item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                        item.processed_flag_2 = ProcessFlag.ProcessingFinished.ToString();
                        item.processed_flag_3 = ProcessFlag.ProcessingFinished.ToString();
                    }
                    gCtx.SaveChanges();
                    if (cn.Equals("2011"))
                    {
                        DownlineDataInstructionTotal.Gen2011ToGateway();
                    }
                    if (cn.Equals("2051"))
                    {
                        DownlineDataInstructionTotal.Gen2051ToGateway();
                    }
                    if (cn.Equals("2061"))
                    {
                        DownlineDataInstructionTotal.Gen2061ToGateway();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        private static void UplineInstructionProcess(string cn, received_msg item, PlatformContext pCtx,
            GatewayMonitorContext gCtx)
        {
            Log.Info("Processing " + cn + ":" + item.received_string);
            switch (item.cn)
            {
                case "2011":
                    UplineDataInstructionMonitor.Process2011(item.received_string);
                    UplineDataInstructionSpecialMonitor.Process2011Hf(item.received_string);
                    UplineAlarmInstruction.Process2011FlagD(item.received_string);
                    UplineDataInstructionMonitor.ProcessRealMonitorBy2011(item.received_string);
                    UplineDataInstructionSpecialMonitor.ProcessRealMonitorBy2011Hf(item.received_string);
                    DownlineFlowTriggerMonitor.Trigger(pCtx, gCtx, item.received_string);
                    Alarmer.TriggerByConcentration(pCtx, gCtx, item.received_string);
                    break;
                case "2051":
                    UplineDataInstructionMonitor.Process2051(item.received_string);
                    UplineDataInstructionSpecialMonitor.Process2051Hf(item.received_string);
                    UplineDataInstructionMonitor.ProcessRealMonitorBy2051(item.received_string);
                    UplineDataInstructionSpecialMonitor.ProcessRealMonitorBy2051Hf(item.received_string);
                    break;
                case "2061":
                    UplineDataInstructionMonitor.Process2061(item.received_string);
                    UplineDataInstructionSpecialMonitor.Process2061Hf(item.received_string);
                    UplineDataInstructionMonitor.ProcessRealMonitorBy2061(item.received_string);
                    UplineDataInstructionSpecialMonitor.ProcessRealMonitorBy2061Hf(item.received_string);
                    Alarmer.TriggerByMonthTotal(pCtx, gCtx, item.received_string);
                    //todo:当日统计，目前是不统计
                    //var datatime = Parser.GetDateTimeByKey("DataTime", item.received_string);
                    //var now = DateTime.Now.AddMinutes(10);
                    //if (datatime != null)
                    //{
                    //    var days = (now - datatime.Value).Days;
                    //    if (days == 1)
                    //    {
                    //        UplineDataInstructionMonitor.SumDayDataFromHourDataTriggerBy2061(item.received_string);
                    //        UplineDataInstructionMonitor.SumRealMonitorDayDataFromHourDataTriggerBy2061(item.received_string);
                    //    }
                    //}
                    break;
                case "2031":
                    UplineDataInstructionMonitor.Process2031(item.received_string);
                    UplineDataInstructionSpecialMonitor.Process2031Hf(item.received_string);
                    UplineDataInstructionMonitor.ProcessRealMonitorBy2031(item.received_string);
                    UplineDataInstructionSpecialMonitor.ProcessRealMonitorBy2031Hf(item.received_string);
                    UplineDataInstructionMonitor.SumMonthDataFromDayDataTriggerBy2031(item.received_string);
                    UplineDataInstructionMonitor.SumYearDataFromDayDataTriggerBy2031(item.received_string);
                    UplineDataInstructionMonitor.SumRealMonitorMonthDataFromDayDataTriggerBy2031(item.received_string);
                    UplineDataInstructionMonitor.SumRealMonitorYearDataFromDayDataTriggerBy2031(item.received_string);
                    UplineDataInstructionMonitor.SumRealMonitorTotalDataFromDayDataTriggerBy2031(item.received_string);
                    break;
              
                case "2072":
                    UplineAlarmInstruction.Process2072(pCtx, item.received_string);
                    break;
                case "9011":
                    UplineResponseInstructionBoth.Process9011(pCtx, item.received_string);
                    break;
                case "9012":
                    UplineResponseInstructionBoth.Process9012(pCtx, item.received_string);
                    break;
                case "9014":
                    //UplineResponseInstructionBoth.Process9014(pCtx, item.received_string);
                    break;
                case "3071":

                    UplineStatusInstruction.Process3071ForAlarm(pCtx, item.received_string);
                    UplineStatusInstruction.Process3071ForResponse(pCtx, item.received_string);
                    UplineStatusInstruction.Process3071ForMonitor(pCtx, item.received_string);
                    UplineStatusInstruction.Process3071ForState(pCtx, item.received_string);
                    UplineStatusInstruction.Process3071ForHistory(pCtx, item.received_string);
                    break;
                case "3097":
                    UplineStatusInstruction.Process3097ForResponse(pCtx, item.received_string);
                    UplineStatusInstruction.Process3097ForMonitor(pCtx, item.received_string);
                    UplineStatusInstruction.Process3097ForState(pCtx, item.received_string);
                    UplineStatusInstruction.Process3097ForHistory(pCtx, item.received_string);
                    break;
                case "3081":
                    UplineStatusInstruction.Process3081ForMonitor(pCtx, item.received_string);
                    UplineStatusInstruction.Process3081ForHistory(pCtx, item.received_string);
                    break;
                case "3082":
                    UplineStatusInstruction.Process3082ForMonitor(pCtx, item.received_string);
                    UplineStatusInstruction.Process3082ForHistory(pCtx, item.received_string);
                    break;
                case "1011":
                    UplineCmdInstructionBoth.Process1011(pCtx, item.received_string);
                    break;
                case "3052":
                    UplineCmdInstructionTotal.Process3052(pCtx, item.received_string);
                    break;
                case "4012":
                    UplineStatusInstruction.Process4012ForState(pCtx, item.received_string);
                    UplineStatusInstruction.Process4012ForHistory(pCtx, item.received_string);
                    break;
            }
            if (item.cn.Equals("0000"))
            {
                UplineStatusInstruction.Process0000(pCtx, item.received_string);
            }
            else
            {
                UplineStatusInstruction.Process9999(pCtx, item.received_string);
            }

        }
    }
}
